package com.shuai.adapter;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuai.model.MemoryListItem;
import com.shuai.stickynote.MemoryNote;
import com.shuai.stickynote.R;
import com.shuai.tool.IDManager;

import java.util.ArrayList;

public class MemoryEditAdapter extends BaseAdapter {
    private static final String TAG = "fuck";
    private Context context;
    private ArrayList<MemoryListItem> data;
    private ArrayList<ViewHolder> holders;
    private ViewHolder theLastHolder;
    private ArrayList<MemoryNote> memory_new, memory_update;
    private ArrayList<String> memory_delete;
    private String group = "";

    public MemoryEditAdapter(Context context) {
        this.context = context;
        if (data == null) data = new ArrayList<>();
        if (holders == null) holders = new ArrayList<>();
        if (memory_update == null) memory_update = new ArrayList<>();
        if (memory_new == null) memory_new = new ArrayList<>();
        if (memory_delete == null) memory_delete = new ArrayList<>();
    }
    public void addNewNote(){
        if (data == null) data = new ArrayList<>();
        data.add(new MemoryListItem());
        notifyDataSetChanged();
    }
    public void addMemory(ArrayList<MemoryNote> memorys){
        if (memorys == null) return;
        if (data == null) data = new ArrayList<>();
        if (holders == null) holders = new ArrayList<>();
        if (memory_update == null) memory_update = new ArrayList<>();
        data.clear();
        holders.clear();
        group = memorys.get(0).getGroup();
        for (MemoryNote note : memorys) {
            data.add(new MemoryListItem(note.getEn(),note.getCh(),note.getId()));
        }
        notifyDataSetChanged();
    }
    public void deleteAll(){
        data.clear();
        holders.clear();
        addNewNote();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (holders == null) holders = new ArrayList<>();
        final ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.adapter_memory_edit_list,null);
            holder = new ViewHolder();
            holder.btn_add = convertView.findViewById(R.id.adapter_memory_btn_add);
            holder.btn_delete = convertView.findViewById(R.id.adapter_memory_btn_delete);
            holder.et_en = convertView.findViewById(R.id.adapter_memory_en);
            holder.et_ch = convertView.findViewById(R.id.adapter_memory_ch);
            holders.add(holder);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
            holder.et_en.requestFocus();
        }
        if ((data.size()-1) == position) theLastHolder = holder;
        holder.et_en.setText(data.get(position).getEn());
        holder.et_ch.setText(data.get(position).getCh());
        if (data.get(position).isNewNote()){
            holder.btn_add.setVisibility(View.VISIBLE);
            holder.btn_delete.setVisibility(View.GONE);
        }else {
            holder.btn_add.setVisibility(View.GONE);
            holder.btn_delete.setVisibility(View.VISIBLE);
        }
        holder.et_en.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                String en = holder.et_en.getText().toString();
                if (!en.equals("")){
                    holder.et_en.clearFocus();
                    holder.et_ch.requestFocus();
                }
                return true;
            }
        });
        holder.et_ch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                String en = holder.et_en.getText().toString();
                String ch = holder.et_ch.getText().toString();
                if (data.get(position).isNewNote() && !en.equals("") && !ch.equals("")){
                    data.get(position).setEn(en);
                    data.get(position).setCh(ch);
                    data.get(position).setNewNote(false);
                    addNewNote();
                    holder.et_en.clearFocus();
                    holder.et_ch.clearFocus();
                    notifyDataSetChanged();
                }
                return true;
            }
        });
        holder.btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String en = holder.et_en.getText().toString();
                String ch = holder.et_ch.getText().toString();
                if (data.get(position).isNewNote() && !en.equals("") && !ch.equals("")){
                    data.get(position).setEn(en);
                    data.get(position).setCh(ch);
                    data.get(position).setNewNote(false);
                    addNewNote();
                    holder.et_en.clearFocus();
                    holder.et_ch.clearFocus();
                    notifyDataSetChanged();
                }
            }
        });
        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (memory_delete == null) memory_delete = new ArrayList<>();
                if (!data.get(position).getMemoryId().equals(""))
                    memory_delete.add(data.get(position).getMemoryId());
                data.get(data.size()-1).setEn(theLastHolder.et_en.getText().toString());
                data.get(data.size()-1).setCh(theLastHolder.et_ch.getText().toString());
                data.remove(position);
                Log.d(TAG, "onClick: "+memory_delete.size());
                notifyDataSetChanged();
            }
        });
        return convertView;
    }
    public boolean sortData(){
        if (group.equals("")) group = IDManager.getID();
        for (int i = 0; i < data.size(); i++) {
            String id = data.get(i).getMemoryId();
            String en = holders.get(i).et_en.getText().toString();
            String ch = holders.get(i).et_ch.getText().toString();
            if (!en.equals("")) {
                if (!id.equals(""))
                    memory_update.add(new MemoryNote(id,en,ch,group));
                else memory_new.add(new MemoryNote(en,ch,group));
            }
        }
        if (memory_new.size() > 0 || memory_update.size() > 0) return true;
        return false;
    }
    public ArrayList<MemoryNote> getMemory_new() {
        return memory_new;
    }
    public ArrayList<MemoryNote> getMemory_update() {
        return memory_update;
    }
    public ArrayList<String> getMemory_delete() {
        return memory_delete;
    }
    static class ViewHolder {
        ImageView btn_add, btn_delete;
        EditText et_en, et_ch;
    }
}
